package com.cmo.rzl.shine.controller;

import java.util.Date;
import java.util.List;

import com.cmo.rzl.core.jfinal.ext.kit.JsonKit;
import com.cmo.rzl.shine.model.DataConnection;
import com.cmo.rzl.shine.model.DbSql;
import com.cmo.rzl.shine.service.DataConnectionService;
import com.cmo.rzl.shine.service.DbSqlService;
import com.cmo.rzl.shine.service.impl.DataConnectionServiceImpl;
import com.cmo.rzl.shine.service.impl.DbSqlServiceImpl;
import com.cmo.rzl.system.controller.base.UrlPermissController;

/**
 * Generated by Blade.
 * 2017-08-16 09:50:09
 */
public class DbSqlController extends UrlPermissController {
	private static String CODE = "dbSql";
	private static String PERFIX = "db_sql";
	private static String LIST_SOURCE = "DbSql.list";
	private static String BASE_PATH = "/platform/dbSql/";
	
	DataConnectionService dataservice=new DataConnectionServiceImpl();
	DbSqlService service = new DbSqlServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "dbSql.html");
	}

	public void add() {
		//查询数据源
		List<DataConnection> list= dataservice.listDesc();
		setAttr("listDesc", list);
		setAttr("code", CODE);
		render(BASE_PATH + "dbSql_add.html");
	}

	public void edit() {
		//查询数据源
		List<DataConnection> list= dataservice.listDesc();
		setAttr("listDesc", list);
		String id = getPara(0);
		DbSql dbSql = service.findById(id);
		setAttr("model", JsonKit.toJson(dbSql));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "dbSql_edit.html");
	}

	public void view() {
		String id = getPara(0);
		DbSql dbSql = service.findById(id);
		setAttr("model", JsonKit.toJson(dbSql));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "dbSql_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		DbSql dbSql = mapping(PERFIX, DbSql.class);
		//dbSql.setLast_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		dbSql.setLast_time(new Date());
		boolean temp = service.save(dbSql);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		DbSql dbSql = mapping(PERFIX, DbSql.class);
		
		boolean temp = service.update(dbSql);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
